package com.cdn.log.config;

/**
 * 蔡定努
 * 2025/03/10 15:38
 */

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

public class ThreadPoolConfig {

    @Autowired
    private  ClogExecutorProperties clogExecutorProperties;


    @Bean("clogExecutor")
    public ThreadPoolTaskExecutor asyncExecutor(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(clogExecutorProperties.getCorePoolSize());
        executor.setMaxPoolSize(clogExecutorProperties.getMaxPoolSize());
        executor.setQueueCapacity(clogExecutorProperties.getQueueCapacity());
        executor.setKeepAliveSeconds(clogExecutorProperties.getKeepAliveTime());
        executor.setThreadNamePrefix(clogExecutorProperties.getThreadNamePrefix());
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.setTaskDecorator(new ClogMdcTaskDecorator());
        executor.initialize();
        return executor;
    }
}

